package com.github.wisdom.admin.controller;

import com.github.wisdom.admin.entity.AddressEntity;
import com.github.wisdom.admin.entity.queryEntity.PagedQueryAddressEntity;
import com.github.wisdom.admin.service.AddressService;
import com.github.wisdom.common.github.modules.util.PageUtils;
import com.github.wisdom.common.github.modules.util.QueryEntityTools;
import com.github.wisdom.common.github.modules.util.ReturnEntity;
import com.github.wisdom.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 地址
 *
 * @author root
 * @email admin@gmail.com
 * @date 2018-09-13 13:55:52
 */
@Api(value = "地址",description="地址")
@RestController
@RequestMapping("/address/addresss")
public class AddressController extends BaseController {
    @Autowired
    private AddressService<AddressEntity> addressService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @GetMapping("")
    public ReturnEntity<PageUtils<AddressEntity>> list(PagedQueryAddressEntity address){
    	Map<String, Object> params = QueryEntityTools.beanToMap(address);
		PageUtils<AddressEntity> page = addressService.queryPage(params);
        return new ReturnEntity<PageUtils<AddressEntity>>().ok(page);
    }


    /**
     * 信息
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ReturnEntity<AddressEntity> info(@PathVariable("id") String id){
    	AddressEntity address = addressService.selectById(id);
		return new ReturnEntity<AddressEntity>().ok(address);
    }

     /**
     * 创建
     */
    @ApiOperation("创建")
    @PostMapping("")
    public ReturnEntity<AddressEntity> save(@RequestBody AddressEntity address){
		addressService.insert(address);
        return new ReturnEntity<AddressEntity>().ok(address);
    }

     /**
     * 修改
     */
    @ApiOperation("修改")
    @PutMapping("/{id}")
    public ReturnEntity<AddressEntity> update(@PathVariable("id") String id,@RequestBody AddressEntity address){
		if (id == null || id == "") {
			return new ReturnEntity<AddressEntity>().error("id不能为空");
		} else {
			if (id.equals(address.getId())) {
			addressService.updateById(address);
			return new ReturnEntity<AddressEntity>().ok(address);
		} else {
			return new ReturnEntity<AddressEntity>().error("id不匹配");
		}
						}
    }
    /**
     * 批量删除
     */
    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public ReturnEntity delete(@RequestBody String[] ids){
		addressService.deleteBatchIds(Arrays.asList(ids));
        return new ReturnEntity().ok();
    }

   /**
	 * 删除
	 */
	@ApiOperation("删除")
	@DeleteMapping("/{id}")
    public ReturnEntity delete(@PathVariable("id") String id){
		addressService.deleteById(id);
		return new ReturnEntity().ok();
    }

}
